package com.example.train.business.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.example.train.business.domain.Daily_TrainSeat;
import com.example.train.business.domain.Daily_TrainSeatExample;
import com.example.train.business.domain.Train_Seat;
import com.example.train.business.domain.Train_Station;
import com.example.train.business.mapper.Daily_TrainSeatMapper;
import com.example.train.business.req.Daily_TrainSeatQueryReq;
import com.example.train.business.req.Daily_TrainSeatSaveReq;
import com.example.train.business.resp.Daily_TrainSeatQueryResp;
import com.example.train.common.resp.PageResp;
import com.example.train.common.util.SnowUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class Daily_TrainSeatService {
    private static final Logger LOG = LoggerFactory.getLogger(Daily_TrainSeatService.class);

    @Resource
    private Daily_TrainSeatMapper daily_TrainSeatMapper;

    @Resource
    private Train_SeatService train_SeatService;

    @Resource
    private Train_StationService train_StationService;

    /**
     * 乘客注册请求的服务类
     * @param req
     */
    public void save(Daily_TrainSeatSaveReq req) {
        DateTime now = DateTime.now();
        Daily_TrainSeat daily_TrainSeat = BeanUtil.copyProperties(req, Daily_TrainSeat.class);
        if(ObjectUtil.isNull(daily_TrainSeat.getId())){
            //在service中对以下三种数据赋值，因为请求参数不带这三个值，需要服务端自行设置
            daily_TrainSeat.setId(SnowUtil.getSnowflakeNextId());
            daily_TrainSeat.setCreateTime(now);
            daily_TrainSeat.setUpdateTime(now);
            daily_TrainSeatMapper.insert(daily_TrainSeat);
        }else{
            daily_TrainSeat.setUpdateTime(now);
            daily_TrainSeatMapper.updateByPrimaryKey(daily_TrainSeat);
        }
    }
    public PageResp<Daily_TrainSeatQueryResp> queryList(Daily_TrainSeatQueryReq req) {
        Daily_TrainSeatExample daily_TrainSeatExample = new Daily_TrainSeatExample();
        daily_TrainSeatExample.setOrderByClause("date asc,id asc");
        Daily_TrainSeatExample.Criteria criteria = daily_TrainSeatExample.createCriteria();
        if(ObjectUtil.isNotEmpty(req.getTrainCode())){
            criteria.andTrainCodeEqualTo(req.getTrainCode());
        }
        if(ObjectUtil.isNotNull(req.getDate())){
            criteria.andDateEqualTo(req.getDate());
        }

        LOG.info("查询页码是 {}", req.getPage());
        LOG.info("每页条数是 {}", req.getSize());
        PageHelper.startPage(req.getPage(), req.getSize());
        List<Daily_TrainSeat> daily_TrainSeatList = daily_TrainSeatMapper.selectByExample(daily_TrainSeatExample);

        PageInfo<Daily_TrainSeat> pageInfo = new PageInfo<>(daily_TrainSeatList);
        LOG.info("总行数是 {}", pageInfo.getTotal());
        LOG.info("总页数是 {}", pageInfo.getPages());

        List<Daily_TrainSeatQueryResp> list = BeanUtil.copyToList(daily_TrainSeatList, Daily_TrainSeatQueryResp.class);

        PageResp<Daily_TrainSeatQueryResp> pageResp = new PageResp<>();
        pageResp.setTotal(pageInfo.getTotal());
        pageResp.setList(list);
        return pageResp;
    }

    /**
     * 删除乘客信息的服务实现
     * @param id
     */
    public void delete(Long id) {
        daily_TrainSeatMapper.deleteByPrimaryKey(id);
    }


    public void genDaily(Date date, String trainCode) {
        LOG.info("生成日期【{}】车次【{}】的座位信息开始", DateUtil.formatDate(date), trainCode);

        // 删除某日某车次的座位信息
        Daily_TrainSeatExample dailyTrainSeatExample = new Daily_TrainSeatExample();
        dailyTrainSeatExample.createCriteria()
                .andDateEqualTo(date)
                .andTrainCodeEqualTo(trainCode);
        daily_TrainSeatMapper.deleteByExample(dailyTrainSeatExample);

        List<Train_Station> stationList = train_StationService.selectByTrainCode(trainCode);
        String sell = StrUtil.fillBefore("", '0', stationList.size() - 1);

        // 查出某车次的所有的座位信息
        List<Train_Seat> seatList = train_SeatService.selectByTrainCode(trainCode);
        if (CollUtil.isEmpty(seatList)) {
            LOG.info("该车次没有座位基础数据，生成该车次的座位信息结束");
            return;
        }

        for (Train_Seat trainSeat : seatList) {
            DateTime now = DateTime.now();
            Daily_TrainSeat dailyTrainSeat = BeanUtil.copyProperties(trainSeat, Daily_TrainSeat.class);
            dailyTrainSeat.setId(SnowUtil.getSnowflakeNextId());
            dailyTrainSeat.setCreateTime(now);
            dailyTrainSeat.setUpdateTime(now);
            dailyTrainSeat.setDate(date);
            dailyTrainSeat.setSell(sell);
            daily_TrainSeatMapper.insert(dailyTrainSeat);
        }
        LOG.info("生成日期【{}】车次【{}】的座位信息结束", DateUtil.formatDate(date), trainCode);
    }

    /**
     * 某种类型座位的计数功能
     * @param date
     * @param trainCode
     * @param seatType
     * @return
     */
    public int countSeat(Date date, String trainCode,String seatType) {
        Daily_TrainSeatExample dailyTrainSeatExample = new Daily_TrainSeatExample();
        dailyTrainSeatExample.createCriteria().
                andDateEqualTo(date)
                .andTrainCodeEqualTo(trainCode)
                .andSeatTypeEqualTo(seatType);
        long l= daily_TrainSeatMapper.countByExample(dailyTrainSeatExample);
        if(l==0L){
            return -1;
        }
        return (int) l;
    }

    /**
     * 根据车厢查询数据
     * @param date
     * @param trainCode
     * @param carriageIndex
     * @return
     */
    public List<Daily_TrainSeat> selectByCarriage(Date date, String trainCode, Integer carriageIndex) {
        Daily_TrainSeatExample example = new Daily_TrainSeatExample();
        example.setOrderByClause("carriage_seat_index asc");
        example.createCriteria()
                .andDateEqualTo(date)
                .andTrainCodeEqualTo(trainCode)
                .andCarriageIndexEqualTo(carriageIndex);
        return daily_TrainSeatMapper.selectByExample(example);
    }

}

